package io.wdb.modules.activity.wx.controller;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.wdb.common.utils.DateFormatUtil;
import io.wdb.common.utils.LeadExportUtil;
import io.wdb.common.utils.PageUtils;
import io.wdb.common.utils.Query;
import io.wdb.common.utils.R;
import io.wdb.modules.activity.wx.entity.MarketPrizeEntity;
import io.wdb.modules.activity.wx.entity.MarketUserPrizeEntity;
import io.wdb.modules.activity.wx.service.MarketUserPrizeService;

/**
 * 
 * 
 * @author wdb
 * @email admin@wdb007.com
 * @date 2017-11-24 09:53:27
 */
@RestController
@RequestMapping("marketuserprize")
public class MarketUserPrizeController {
	@Autowired
	private MarketUserPrizeService marketUserPrizeService;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("marketuserprize:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<MarketUserPrizeEntity> marketUserPrizeList = marketUserPrizeService.queryList(query);
		int total = marketUserPrizeService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(marketUserPrizeList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("marketuserprize:info")
	public R info(@PathVariable("id") Long id){
		MarketUserPrizeEntity marketUserPrize = marketUserPrizeService.queryObject(id);
		
		return R.ok().put("marketUserPrize", marketUserPrize);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("marketuserprize:save")
	public R save(@RequestBody MarketUserPrizeEntity marketUserPrize){
		marketUserPrizeService.save(marketUserPrize);
		
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("marketuserprize:update")
	public R update(@RequestBody MarketUserPrizeEntity marketUserPrize){
		marketUserPrizeService.update(marketUserPrize);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("marketuserprize:delete")
	public R delete(@RequestBody Long[] ids){
		marketUserPrizeService.deleteBatch(ids);
		
		return R.ok();
	}
	@RequestMapping("/export")
	//@RequiresPermissions("marketuserprize:export")
	public void exportUserPrize(HttpServletRequest request, HttpServletResponse response,Long[] ids,Long user_id) {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;
		try {
			//List<MarketPrizeEntity> marketPrizeEntities = marketUserPrizeService.getAllPrizes();
			List<MarketUserPrizeEntity> marketUserPrizeEntities = null;
			if(null!=ids&&ids.length>0) {
				marketUserPrizeEntities = marketUserPrizeService.getUserPrizeEntitiesByIds(ids);
			}
			if(null!=user_id&&!"".equals(user_id)) {
				marketUserPrizeEntities = marketUserPrizeService.getUserPrizeEntitiesByUserId(user_id);
			}
			if(marketUserPrizeEntities==null) {
				marketUserPrizeEntities = marketUserPrizeService.queryList(null);
			}
			System.out.println("MarketUserPrizeController.exportUserPrize()");
			String fileName = "export-prize";
			//先生成要导出的xls文件，并返回文件存储目录
			String filePath = LeadExportUtil.intoServerXls(marketUserPrizeEntities,fileName);
			//下面是读取生成的文件并进行下载操作
			response.setContentType("application/x-msdownload");  
            response.setCharacterEncoding("utf-8");  
            response.setHeader("Content-disposition", "attachment;filename="+fileName+DateFormatUtil.formatDateTime(new Date())+".xls");  
            // 通知客户文件的MIME类型：  
            bis = new BufferedInputStream(new FileInputStream((filePath)));
            bos = new BufferedOutputStream(response.getOutputStream()); 
            byte[] buff = new byte[2048];  
            int bytesRead;
            int i = 0;
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {  
                bos.write(buff, 0, bytesRead);  
                i++;  
            }
            bos.flush();
		}catch (Exception e) {
			e.printStackTrace();
		}finally {  
            if (bis != null) {  
                try {  
                    bis.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                bis = null;  
            }  
            if (bos != null) {  
                try {  
                    bos.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
                bos = null;  
            }  
        } 
	}
}
